<?php 

class myMenu {
	public $items = array();
	var $maxLevel =0;
	var $cssCreated = false;
	var $defImageHeight = "";
	
	function addManuItem($text, $parent = null, $link = "", $classes =""){
		$item = new myMenuItem();
		if($parent == null)
			$item->level = 0;
		else
			$item->level = $parent->level+1;
		
		if($this->maxLevel < $item->level)
			$this->maxLevel = $item->level;
				
		$item->link = $link;
		$item->text = $text;
		if($parent == null)
			$this->items[] = $item;
		else
			$parent->items[] = $item;
		$item->imageHeight = $this->defImageHeight;
		$item->classes = $classes;
		return $item;
	}	
	
	function createcss(){
		$this->cssCreated = true;
		echo "<link rel=\"stylesheet\" href=\"styles/default/menu.css\" >";
		echo "<style type=\"text/css\">\n";
		$line1 = "";
		$line2 = "";
		for($i=0;$i<$this->maxLevel;$i++){
			$line1 .= ($i==0?"":", ") . "li.menuItem" . $i .":hover ul.submenu" . $i;
			//$line2 .= ($i==0?"":", ") . "a.menuItem" . $i . ", li.menuItem" . $i .":hover li.menuItem" . ($i+1) ." a.menuItem".($i+1); 
		}
		echo $line1 . " { display: block; }\n";
		//echo $line2 . " { display: block; z-index:" . (101 + $i). ";}\n";
		echo "</style>";
	}
	
	function display(){
		if(!$this->cssCreated)
			$this->createcss();
			
		//print_r($this->items);
		echo "<div>";
		
		echo "<ul class=\"menu\" >";
		foreach ($this->items as &$item){
			$item->display();
		}
		echo "</ul>";
		echo "</div>";
	}	
}

class myMenuItem {
	 
	var $level = 0;
	var $text = "";
	var $link = "";
	var $image = "";
	var $imageWidth ="";
	var $imageHeight = "";
	var $classes = "";
	var $items = array();
	function is_empty($what){
		if ($what == null || "" == $what)
			return true;
		return false;
	}
	
	function display(){
		$children = count($this->items);
		
		$liItem = "<li class=\"menuItem" . $this->level;
		if(!$this->is_empty($this->classes)){
			$liItem .= " " . $this->classes;
		}
		$liItem .= "\"";
		$tableItem ="";
		if( !($this->is_empty($this->image))) {
			if(!$this->is_empty($this->imageHeight) || !$this->is_empty($this->imageWidth)){
				$liItem .= " style=\"";
			}
			$tableItem = "<table><tr><td><img src=\"" . $this->image. "\"";
			if(!($this->is_empty($this->link)))
				$tableItem .= " onclick=\"javascript:window.location.href='". $this->link . "'\"";
			if(!$this->is_empty($this->imageWidth)){
				$liItem .= " width: " . $this->imageWidth .";";
				$tableItem .= " width=\"" . $this->imageWidth . "\"";
			} else {
				$tableItem .= " width=\"auto\"";
			}
			if(!$this->is_empty($this->imageHeight)) {
				$liItem .= " height: " . $this->imageHeight .";";
				$tableItem .= " height=\"" . $this->imageHeight . "\"";
			} else {
				$tableItem .= " height=\"inherit\"";
			}
			if(!$this->is_empty($this->imageHeight) || !$this->is_empty($this->imageWidth)){
				$liItem .= "\"";
			}
			$tableItem .= " ></td><td>";
		}
		$liItem .= "/>";
		echo $liItem . $tableItem;
		echo "<a class=\"menuItem" . $this->level . "\" ";
		
		if (!($this->is_empty($this->link)))
			echo "href=\"" . $this->link . "\"";
		
		echo  ">" . $this->text . "</a>";
		
		if($children > 0){
			echo "<ul class=\"submenu" . $this->level . "\" >";
			foreach ($this->items as $item){
				$item->display();
			}
			echo "</ul>";
		}
			if( !($this->is_empty($this->image))) {
				echo "</td></tr></table>";
			}
		echo "</li>";
	}

}

?>
